package com.obtk.controllerSW;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.model.OrderLine;
import com.obtk.service.OrderLineService;
import com.obtk.util.CoreConst;
import com.obtk.util.PageUtil;
import com.obtk.util.ResultUtil;
import com.obtk.vo.base.PageResultVo;
import com.obtk.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 功能说明
 *
 * @author Mr.Sun
 * @date 2023-04-03 17:11:50
 */
@Controller
@RequestMapping("/orderLine")
public class OrderLineController {
    @Autowired
    private OrderLineService orderLineService;
    @PostMapping("/list")
    @ResponseBody
    public PageResultVo loadUsers(OrderLine orderLine, Integer limit, Integer offset, Model model){
        PageHelper.startPage(PageUtil.getPageNo(limit, offset),limit);
        List<OrderLine> orderLineList = orderLineService.selectOrderLine(orderLine);
        PageInfo<OrderLine> pages = new PageInfo<>(orderLineList);
        System.out.println(orderLineList);
        return ResultUtil.table(orderLineList,pages.getTotal());
    }
    /**新增订单或产品*/



    /**编辑用户详情*/
    @GetMapping("/edit")
    public String customerDetail(Model model, Integer oddId){
        OrderLine orderLine = orderLineService.selectByOddProdId(oddId);
        model.addAttribute("orderLine", orderLine);

        return "orderLine/update";
    }

    /**编辑用户*/
    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo editUser(OrderLine orderLineForm){
        int a = orderLineService.updateByOddId(orderLineForm);
        if (a > 0) {
            return ResultUtil.success("编辑成功！");
        } else {
            return ResultUtil.error("编辑失败");
        }
    }
    /**删除用户*/
    @GetMapping("/delete")
    @ResponseBody
    public ResponseVo deleteUser(String oddId) {
        List<String> orderLineIdsForm = Arrays.asList(oddId);
        int a = orderLineService.updateStatusBatch(orderLineIdsForm, CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除成功");
        } else {
            return ResultUtil.error("删除失败");
        }
    }
    @GetMapping("/batch/delete")
    @ResponseBody
    public ResponseVo batchDeleteUser(String orderLineIdStr) {
        String[] oddIds = orderLineIdStr.split(",");
        List<String> orderLineIdsForm = Arrays.asList(oddIds);
        int a = orderLineService.updateStatusBatch(orderLineIdsForm,CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除成功");
        } else {
            return ResultUtil.error("删除失败");
        }
    }
}
